{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAFVdJREFUeJzt3W+MXOV1x/Hf8WYrNgrJtmKriLUd\nI7VygiCJlRFC8os0htQkQYBoqiYtUaS88JtEgjRZahepEi8qLK2U5EUiRVZbqRJIUBLiNn8qAzJR\nVSSqrDEOoeAIUSAsqbJRcJPCNqzt0xe7i3fHszN3Zp57nz/3+5Es4fEw88y/c889z3mea+4uAEA5\ntsUeAAAgLAI7ABSGwA4AhSGwA0BhCOwAUBgCOwAUhsAOAIUhsANAYQjsAFCYt8V40ssuu8x37doV\n46kBIFsnTpz4pbvPDLpflMC+a9cuLSwsxHhqAMiWmb1U5X6UYgCgMAR2ACgMgR0ACkNgB4DCENgB\noDAEdgAoTJR2RwDpOXpyUfPHTuvVM8u6fHpKc/t365Y9s7GHhREQ2AHo6MlFHXroaS2vnJMkLZ5Z\n1qGHnpakYoJ7mw5clGIAaP7Y6beC+rrllXOaP3Y60ojCWj9wLZ5ZluvCgevoycXYQ6sFgR2AXj2z\nPNTtuSn9wNWNwA5Al09PDXV7bko/cHUjsAPQ3P7dmpqc2HTb1OSE5vbvjjSisEo/cHUjsAPQLXtm\ndc+tV2t2ekomaXZ6SvfcenUxk4ulH7i60RUDQNJqcC8lkHdbf11t6YohsANohZIPXN0oxQBAYQjs\nAFAYAjsAFIbADgCFIbADQGEI7ABQGAI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Y3SvqFu58wsz+KPZ4QCOxjcPfre91uZldLukLS\nKTOTVssST5rZNe7+3w0OMaitXu86M/uspBslXefl9dG+ImnHhr9vl/RqpLHUzswmtRrU73P3h2KP\np2Z7Jd1kZh+XdImkd5rZve5+W+RxjYw+9gaY2YuSOu6e+gZDIzOzGyR9RdKH3X0p9nhCM7O3aXVS\n+DpJi5J+JOnP3f2ZqAOrga1mI/8o6Vfufkfs8TRpLWP/srvfGHss46DGjlC+LulSSY+Y2VNm9s3Y\nAwppbWL4C5KOaXUy8Z9KDOpr9kr6jKR9a5/lU2vZLDJBxg4AhSFjB4DCENgBoDAEdgAoDIEdAApD\nYAeAwhDYAaAwBHYAKAyBHQAK8/9XHp2wI02zHAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x16abf0b0198>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"kmeans.txt\", delimiter=\" \")\n",
    "\n",
    "plt.scatter(data[:,0],data[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(80, 2)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 计算距离 \n",
    "def euclDistance(vector1, vector2):  \n",
    "    return np.sqrt(sum((vector2 - vector1)**2))\n",
    "  \n",
    "# 初始化质心\n",
    "def initCentroids(data, k):  \n",
    "    numSamples, dim = data.shape\n",
    "    # k个质心，列数跟样本的列数一样\n",
    "    centroids = np.zeros((k, dim))  \n",
    "    # 随机选出k个质心\n",
    "    for i in range(k):  \n",
    "        # 随机选取一个样本的索引\n",
    "        index = int(np.random.uniform(0, numSamples))  \n",
    "        # 作为初始化的质心\n",
    "        centroids[i, :] = data[index, :]  \n",
    "    return centroids  \n",
    "  \n",
    "# 传入数据集和k的值\n",
    "def kmeans(data, k):  \n",
    "    # 计算样本个数\n",
    "    numSamples = data.shape[0]   \n",
    "    # 样本的属性，第一列保存该样本属于哪个簇，第二列保存该样本跟它所属簇的误差\n",
    "    clusterData = np.array(np.zeros((numSamples, 2)))  \n",
    "    # 决定质心是否要改变的变量\n",
    "    clusterChanged = True  \n",
    "  \n",
    "    # 初始化质心  \n",
    "    centroids = initCentroids(data, k)  \n",
    "  \n",
    "    while clusterChanged:  \n",
    "        clusterChanged = False  \n",
    "        # 循环每一个样本 \n",
    "        for i in range(numSamples):  \n",
    "            # 最小距离\n",
    "            minDist  = 100000.0  \n",
    "            # 定义样本所属的簇\n",
    "            minIndex = 0  \n",
    "            # 循环计算每一个质心与该样本的距离\n",
    "            for j in range(k):  \n",
    "                # 循环每一个质心和样本，计算距离\n",
    "                distance = euclDistance(centroids[j, :], data[i, :])  \n",
    "                # 如果计算的距离小于最小距离，则更新最小距离\n",
    "                if distance < minDist:  \n",
    "                    minDist  = distance \n",
    "                    # 更新最小距离\n",
    "                    clusterData[i, 1] = minDist\n",
    "                    # 更新样本所属的簇\n",
    "                    minIndex = j  \n",
    "              \n",
    "            # 如果样本的所属的簇发生了变化\n",
    "            if clusterData[i, 0] != minIndex:  \n",
    "                # 质心要重新计算\n",
    "                clusterChanged = True\n",
    "                # 更新样本的簇\n",
    "                clusterData[i, 0] = minIndex\n",
    "  \n",
    "        # 更新质心\n",
    "        for j in range(k):  \n",
    "            # 获取第j个簇所有的样本所在的索引\n",
    "            cluster_index = np.nonzero(clusterData[:, 0] == j)\n",
    "            # 第j个簇所有的样本点\n",
    "            pointsInCluster = data[cluster_index]  \n",
    "            # 计算质心\n",
    "            centroids[j, :] = np.mean(pointsInCluster, axis = 0) \n",
    "#         showCluster(data, k, centroids, clusterData)\n",
    " \n",
    "    return centroids, clusterData  \n",
    "  \n",
    "# 显示结果 \n",
    "def showCluster(data, k, centroids, clusterData):  \n",
    "    numSamples, dim = data.shape  \n",
    "    if dim != 2:  \n",
    "        print(\"dimension of your data is not 2!\")  \n",
    "        return 1  \n",
    "  \n",
    "    # 用不同颜色形状来表示各个类别\n",
    "    mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']  \n",
    "    if k > len(mark):  \n",
    "        print(\"Your k is too large!\")  \n",
    "        return 1  \n",
    "  \n",
    "    # 画样本点  \n",
    "    for i in range(numSamples):  \n",
    "        markIndex = int(clusterData[i, 0])  \n",
    "        plt.plot(data[i, 0], data[i, 1], mark[markIndex])  \n",
    "  \n",
    "    # 用不同颜色形状来表示各个类别\n",
    "    mark = ['*r', '*b', '*g', '*k', '^b', '+b', 'sb', 'db', '<b', 'pb']  \n",
    "    # 画质心点 \n",
    "    for i in range(k):  \n",
    "        plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 20)  \n",
    "  \n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cluster complete!\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHEBJREFUeJzt3X1sXNWZBvDnHcfGnlCI1qSJKPgO\nbaNuUT7I4tLudqVtC6wom4YuUJYyRClItaCtRESqbtlRt6LBWkT6kT/6pVELovJIbVhwss5m1VLS\nalequmQoBBJCA2LtFLoVbrelpg7BH+/+cTPYHt+ZuXf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mGwExFZhsFORGQZBjsRkWUSqYoRkUkA\ny89Pt8d5AFqumWORLD3fLD1XgM/XNI6qtlweN5Fgt52IVP2UJNkiS883S88V4PNNKw7FEBFZhsFO\nRGQZBns0ykk3IGZZer5Zeq4An28qcYydiMgy7LETEVmGwR4xEfmsiKiInJd0W6IkIrtF5DkReVpE\nRkVkVdJtCpuIXCUivxSRF0Tk80m3J0oicqGI/EREjovIMRG5I+k2RU1EukTkSRE5kHRbOsVgj5CI\nXAjgSgBZ2GboUQDrVXUjgBMA7kq4PaESkS4A3wDwYQAXA/i4iFycbKsiNQtgp6q+G8D7AHza8ucL\nAHcAOJ50I8LAYI/W1wB8DoD1Exmq+iNVnT1z9ecALkiyPRG4DMALqvqiqr4B4PsArkm4TZFR1f9V\n1V+c+X0KbuC9LdlWRUdELgDwdwC+k3RbwsBgj4iIbAXwsqoeSbotCbgVwH8k3YiQvQ3ArxZdfwkW\nB91iIlIAsBnAfyfbkkjtgdsJm0+6IWHgZtYdEJEfA1jr8U8lAP8E4G/jbVG0mj1fVd1/5jYluF/j\nK3G2LQbiccz6b2IicjaAhwHsUNU/Jt2eKIjIFgCvqOoTIvKBpNsTBgZ7B1T1Cq/jIrIBwEUAjogI\n4A5L/EJELlPV38TYxFA1er41IrIdwBYAl6t9dbQvAbhw0fULAPw6obbEQkS64YZ6RVUfSbo9EXo/\ngK0icjWAXgDniMiIqt6ccLvaxjr2GIjIOIBBVTV5caGOiMhVAL4K4G9UdTLp9oRNRFbAnRS+HMDL\nAA4DuElVjyXasIiI2yN5EMD/qeqOpNsTlzM99s+q6pak29IJjrFTWL4O4C0AHhWRp0Tk20k3KExn\nJoY/A+CHcCcS99oa6me8H8A2AB86834+daZHSynAHjsRkWXYYycisgyDnYjIMgx2IiLLMNiJiCzD\nYCcisgyDnYjIMgx2IiLLMNiJiCzz/+rpvUCYmJYIAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x16ac1489c88>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 设置k值\n",
    "k = 4  \n",
    "# centroids 簇的中心点 \n",
    "# cluster Data样本的属性，第一列保存该样本属于哪个簇，第二列保存该样本跟它所属簇的误差\n",
    "centroids, clusterData = kmeans(data, k)  \n",
    "if np.isnan(centroids).any():\n",
    "    print('Error')\n",
    "else:\n",
    "    print('cluster complete!')   \n",
    "    # 显示结果\n",
    "showCluster(data, k, centroids, clusterData)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.65077367, -2.79019029],\n",
       "       [ 2.6265299 ,  3.10868015],\n",
       "       [-3.53973889, -2.89384326],\n",
       "       [-2.46154315,  2.78737555]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centroids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 做预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [0, 1],\n",
       "       [0, 1],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 做预测\n",
    "x_test = [0,1]\n",
    "np.tile(x_test,(k,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.65077367,  3.79019029],\n",
       "       [-2.6265299 , -2.10868015],\n",
       "       [ 3.53973889,  3.89384326],\n",
       "       [ 2.46154315, -1.78737555]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 误差\n",
    "np.tile(x_test,(k,1))-centroids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.02660103, 14.3655424 ],\n",
       "       [ 6.89865932,  4.44653198],\n",
       "       [12.52975144, 15.16201536],\n",
       "       [ 6.05919468,  3.19471136]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 误差平方\n",
    "(np.tile(x_test,(k,1))-centroids)**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([21.39214343, 11.34519129, 27.6917668 ,  9.25390604])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 误差平方和\n",
    "((np.tile(x_test,(k,1))-centroids)**2).sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最小值所在的索引号\n",
    "np.argmin(((np.tile(x_test,(k,1))-centroids)**2).sum(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def predict(datas):\n",
    "    return np.array([np.argmin(((np.tile(data,(k,1))-centroids)**2).sum(axis=1)) for data in datas])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 画出簇的作用区域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD8CAYAAABjAo9vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxp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8LDXYymnIAT7\nEgArRORVEXlJRK70ekBOEZHbAZxS1de9HovL7gLwH14PokSzAZxM+7gFBgUgAIjIAgDLALzq7Ugc\n9R0kJk5xrwdSThVeDwAAROQFABdl+dS9SIyxEYm3iVcC+FcRWaQBKecp8Nq+CeCm8o7IOflem6r+\nOPmce5F4u7+9nGNzgWR5LBA/g1aISC2AxwFsUNUur8fjBBG5FUCbqu4Tkeu8Hk85+SLYVfWGXJ8T\nkT8G8EQyyP9LROJI9H1oL9f4SpHrtYnI+wEsBPC6iACJpYr9InKVqr5bxiEWLd/3DQBE5PMAbgVw\nfVBOxHm0AJib9vEcAK0ejcVRIhJDItS3q+oTXo/HQdcCuF1EbgFQBaBORP5JVT/j8bhc5/s6dhH5\nIoBmVf1zEVkC4D8BzDMgKDKIyHEAy1XVhEZFEJGbAXwbwG+raiBOwvmISAUSm8DXAzgF4OcAPq2q\nBz0dWIkkMav4BwDnVHWD1+NS4XSGAAAAhklEQVRxS3LGfo+q3ur1WMohCGvs3wewSER+icSG1edN\nC3VD/Q2AKQCeF5HXROS7Xg+oFMmN4K8A2InEBuO/Bj3Uk64F8FkAq5Lfp9eSM1wKMN/P2ImIyJ4g\nzNiJiMgGBjsRkWEY7EREhmGwExEZhsFORGQYBjsRkWEY7EREhmGwExEZ5r8BqZukAd1wxOQAAAAA\nSUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x16ac1484128>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 获取数据值所在的范围\n",
    "x_min, x_max = data[:, 0].min() - 1, data[:, 0].max() + 1\n",
    "y_min, y_max = data[:, 1].min() - 1, data[:, 1].max() + 1\n",
    "\n",
    "# 生成网格矩阵\n",
    "xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),\n",
    "                     np.arange(y_min, y_max, 0.02))\n",
    "\n",
    "z = predict(np.c_[xx.ravel(), yy.ravel()])# ravel与flatten类似，多维数据转一维。flatten不会改变原始数据，ravel会改变原始数据\n",
    "z = z.reshape(xx.shape)\n",
    "# 等高线图\n",
    "cs = plt.contourf(xx, yy, z)\n",
    "# 显示结果\n",
    "showCluster(data, k, centroids, clusterData)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
